package com.kingyun.gpsinspection.purificationservice.services.iam.innerservice.qhse.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.iam.qhse.AccidentAndEventMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.iam.qhse.AccidentAndEvent;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.IamFileService;
import com.kingyun.gpsinspection.purificationservice.facade.service.iam.qhse.AccidentAndEventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 事故事件服务实现
 * Created by yangsy on 2016/11/22.
 * modify by lutianbo on 2017/1/22
 */
public class AccidentAndEventServiceImpl implements AccidentAndEventService
{
    @Autowired
    private AccidentAndEventMapper eventMapper;
    @Autowired
    private IamFileService iamFileService;

    /**
     * 删除
     * @param accidentAndEventIds 事故事件id集合
     * @return
     */
    @Override
    public int deleteByPrimaryKey(List<String> accidentAndEventIds) {
        if(accidentAndEventIds.size()>0){
            return eventMapper.deleteByPrimaryKey(accidentAndEventIds);
        }
        return 0;
    }

    /**
     * 新增
     * @param record
     * @param uploadFiles 需要上传的文件
     * @param deletedFiles 需要删除的文件
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int insertSelective(AccidentAndEvent record,String uploadFiles,String deletedFiles) {
        // 插入数据成功之后，返回的条数
        int resultCount = 0;
        // 判断数据是否为空
        if(null == record){
            return  resultCount;
        }
        resultCount +=  eventMapper.insertSelective(record);
        // 保存附件
        resultCount += iamFileService.newInsertFile(uploadFiles,deletedFiles,record.getAccidentAndEventId());

        return  resultCount;
    }

    /**
     * 根据id查询事故事件
     * @param accidentAndEventId
     * @return
     */
    @Override
    public AccidentAndEvent selectByPrimaryKey(String accidentAndEventId) {
        if(!"".equals(accidentAndEventId)){
            return  eventMapper.selectByPrimaryKey(accidentAndEventId);
        }
        return null;
    }

    /**
     * 更新
     * @param record
     * @param uploadFiles 需要上传的文件
     * @param deletedFiles 需要删除的文件
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updateByPrimaryKeySelective(AccidentAndEvent record,String uploadFiles,String deletedFiles) {
        // 数据更新成功之后，返回的条数
        int resultCount = 0;
        // 判断数据是否为空
        if(null == record){
            return resultCount;
        }
        resultCount +=  eventMapper.updateByPrimaryKeySelective(record);
        // 保存附件
        resultCount += iamFileService.newInsertFile(uploadFiles,deletedFiles,record.getAccidentAndEventId());

        return  resultCount;
    }

    /**
     * 列表查询
     * @param searchBean
     * @return
     */
    @Override
    public JSONObject accidentList(BootstrapSearchBean searchBean){
        JSONObject jsonObject = new JSONObject();
        // 设置分页
        searchBean.setLimit( searchBean.getLimit() == null ? Integer.valueOf(10) : searchBean.getLimit() );
        searchBean.setOffset( searchBean.getOffset() == null ? Integer.valueOf(0) : searchBean.getOffset() );
        // 总条数
        jsonObject.put("total",eventMapper.queryCount(searchBean));
        // 列表信息
        jsonObject.put("rows", JSON.toJSON(eventMapper.queryList(searchBean)));
        return jsonObject;
    }

    /**
     * 判断事故事件名是否存在
     * @param accName 事故事件名
     * @return true：存在 false:不存在
     */
    @Override
    public boolean isExitByName(String accName)
    {
        return eventMapper.isExitByName(accName) > 0 ? true :false;
    }
}
